/*
 *    Copyright 2009 Daniel Hayes
 *    This file is part of PreCT.
 *
 *    PreCT is free software: you can redistribute it and/or modify
 *    it under the terms of the GNU General Public License as published by
 *    the Free Software Foundation, either version 3 of the License, or
 *    (at your option) any later version.
 *
 *    PreCT is distributed in the hope that it will be useful,
 *    but WITHOUT ANY WARRANTY; without even the implied warranty of
 *    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *    GNU General Public License for more details.
 *
 *    You should have received a copy of the GNU General Public License
 *    along with PreCT.  If not, see <http://www.gnu.org/licenses/>
 */

function DBHelper(prectHost){
	this.host = prectHost;
	
    this.dbName = "ext:PreCTDb";
    this.dbDisplayName = "PreCT Database";
    this.dbSize = null;
    this.version = '1.0.0';
	
	this.db = openDatabase(this.dbName, this.version, this.dbDisplayName, this.dbSize);
    this.setupDb();
}

DBHelper.prototype.setupDb = function(){
	var sql = "CREATE TABLE IF NOT EXISTS 'SOLVES' (DATE_ENTERED INTEGER NOT NULL UNIQUE PRIMARY KEY, SESSION_ID INTEGER NOT NULL, PUZZLE_TYPE VARCHAR NOT NULL, SCRAMBLE VARCHAR, SOLVE_TIME_MILLIS INTEGER NOT NULL, COMMENTS VARCHAR); GO;";
	
	this.db.transaction(
		(function(transaction){
			transaction.executeSql(
				sql,
				[],
				function(transaction, results){
					Mojo.Log.info("spdqbr: Successfully created table")
				},
				function(transaction, error){
					Mojo.Log.error("spdqbr: Could not create table: "+error.message);
				}
			)
		}).bind(this)
	);
}



DBHelper.prototype.dropTable = function(){
	var sql = "DROP TABLE IF EXISTS 'SOLVES'; GO;";
	
	this.db.transaction(
		(function(transaction){
			transaction.executeSql(
				sql,
				[],
				function(transaction, results){
					Mojo.Log.info("spdqbr: Successfully dropped table")
				},
				function(transaction, error){
					Mojo.Log.error("spdqbr: Could not drop table: "+error.message);
				}
			)
		}).bind(this)
	);	
}

DBHelper.prototype.addTime = function(sessionId, puzzle, scramble, solveTime, comments){
	sql = "INSERT INTO \"SOLVES\" VALUES(?,?,?,?,?,?); GO;";
	var dateCreated = (new Date()).getTime();
	
	this.db.transaction(
		(function(transaction){
			transaction.executeSql(
				sql,
				[dateCreated, sessionId, puzzle, scramble, solveTime, comments],
				function(transaction, results){
					Mojo.Log.info("spdqbr: Successfully added time of "+solveTime+" for "+puzzle);
				},
				function(transaction, error){
					Mojo.Log.error("Could not insert time: "+error.message);
				}
			)
		}).bind(this)
	);
}

DBHelper.prototype.deleteLastTime = function(puzzle){
	var sql = "DELETE FROM SOLVES where date_entered = (select max(date_entered) from solves s2 where s2.puzzle_type = ?); GO;";
	
	this.db.transaction(
		(function(transaction){
			transaction.executeSql(
				sql,
				[puzzle],
				function(transaction, results){
					Mojo.Log.info("spdqbr: Successfully deleted last time for: "+puzzle);
				},
				function(transaction, error){
					Mojo.Log.error("Could not delete last time for"+puzzle+": "+error.message);
				}
			)
		}).bind(this)
	);	
}

DBHelper.prototype.deleteTimes = function(puzzle){
	var sql = "DELETE FROM SOLVES where puzzle_type = ?; GO;";
	
	this.db.transaction(
		(function(transaction){
			transaction.executeSql(
				sql,
				[puzzle],
				function(transaction, results){
					Mojo.Log.info("spdqbr: Successfully deleted all times for: "+puzzle);
				},
				function(transaction, error){
					Mojo.Log.error("Could not delete times for"+puzzle+": "+error.message);
				}
			)
		}).bind(this)
	);	
}

DBHelper.prototype.getTimes = function(puzzle){
	var sql = "SELECT SOLVE_TIME_MILLIS SOLVE_TIME_MILLIS FROM SOLVES WHERE PUZZLE_TYPE = ? ORDER BY DATE_ENTERED ASC";
	
	this.db.transaction(
		(function(transaction){
			transaction.executeSql(
				sql,
				[puzzle],
				function(transaction, results){
					Mojo.Log.info("spdqbr: Successfully retrieved "+results.rows.length+" times for: "+puzzle);
					var times = []
					for (var i = 0; i < results.rows.length; i++) {
						times.push(results.rows.item(i)['SOLVE_TIME_MILLIS']);
					}
					this.host.setTimes(times);
				}.bind(this),
				function(transaction, error){
					Mojo.Log.error("Could not get times for "+puzzle+": "+error.message);
				}
			)
		}).bind(this)
	);		
}
